const lazyload = {
  mounted(el, binding) {
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            el.src = binding.value
            observer.unobserve(el)
          }
        })
      },
      {
        rootMargin: '50px',
        threshold: 0.01
      }
    )
    
    observer.observe(el)
    
    // 保存observer实例以便后续清理
    el._lazy_observer = observer
  },
  
  beforeUnmount(el) {
    // 清理observer
    if (el._lazy_observer) {
      el._lazy_observer.unobserve(el)
      el._lazy_observer = null
    }
  }
}

export default lazyload 